﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_FieldValueController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回应用字段值列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
            string m = (string)Utils.GetQueryAndFormString("m", MethodType.Get,10, 0, DataType.CharAndNum);
            IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
            List<Sys_FieldValueEntity> sys_fieldvalueList = null;
            if (m == "list")
            {

                string code = (string)Utils.GetQueryAndFormString("code", MethodType.Get, 20, 1, DataType.CharAndNum);
                QueryParam qp = new QueryParam();
                qp.PageSize = int.MaxValue;
                qp.PageIndex = 1;
                qp.Where = string.Concat("V_F_Key='", code, "'");
                qp.Orderfld = "V_ShowOrder desc ";
                int recordNum = 0;

                sys_fieldvalueList = Sys_FieldValueSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(sys_fieldvalueList), "}");
            }
            else if (m == "checkbox")
            {
                string code = (string)Utils.GetQueryAndFormString("code", MethodType.Get, 20, 1, DataType.CharAndNum);
                QueryParam qp = new QueryParam();
                qp.PageSize = int.MaxValue;
                qp.PageIndex = 1;
                qp.Where = string.Concat("V_F_Key='", code, "'");
                qp.Orderfld = "V_ShowOrder desc ";
                int recordNum = 0;

                sys_fieldvalueList = Sys_FieldValueSv.List(qp, out recordNum);
                returnJson = "[";
                for (int i = 0; i < sys_fieldvalueList.Count; i++)
                {
                    if (i != (sys_fieldvalueList.Count - 1))
                    {
                        returnJson = string.Concat(returnJson, "{boxLabel: '", sys_fieldvalueList[i].V_Text, "', name: 'checkboxgrup", code, "',inputValue: '", sys_fieldvalueList[i].V_Code, "'},");
                    }
                    else
                    {
                        returnJson = string.Concat(returnJson, "{boxLabel: '", sys_fieldvalueList[i].V_Text, "', name: 'checkboxgrup", code, "',inputValue: '", sys_fieldvalueList[i].V_Code, "'}");
                    }
                }
                returnJson=string.Concat(returnJson,"]");
            }
            else
            {
                int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
                int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
                string sort = (string)Utils.GetQueryAndFormString("sort", this, 100, 0, DataType.Str);
                QueryParam qp = new QueryParam();
                qp.Where = string.Concat("V_F_Key='", actionId, "'");
                if (!string.IsNullOrEmpty(sort))
                {
                    SortEntity sortEntity = null;
                    List<SortEntity> sortList = new List<SortEntity>();
                    sortList = (List<SortEntity>)JsonConvert.DeserializeObject(sort, typeof(List<SortEntity>));
                    if (sortList.Count > 0)
                    {
                        sortEntity = sortList[0];
                    }

                    qp.PageIndex = pageIndex / pageSize + 1;
                    qp.PageSize = pageSize;
                    if (sortEntity != null)
                        qp.Orderfld = string.Concat(sortEntity.Property, " ", sortEntity.Direction);
                }
                int recordNum = 0;
                sys_fieldvalueList = Sys_FieldValueSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(sys_fieldvalueList), "}");

            }
            return returnJson;
 
        }

        /// <summary>
        /// 查看应用字段值 
        /// </summary>
        /// <param actionId
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
           int intId = Utils.StringToInt(actionId,0);
            Sys_FieldValueEntity sys_fieldvalue = Sys_FieldValueSv.Look(intId);
            if (sys_fieldvalue != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(sys_fieldvalue), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新应用字段值
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Sys_FieldValueEntity sys_fieldvalue = new Sys_FieldValueEntity();
                sys_fieldvalue.DataTable_Action = DataTableAction.Update;
                sys_fieldvalue.ValueID = intId;
                sys_fieldvalue.V_F_Key = (String)Utils.GetQueryAndFormString("V_F_Key", this, 50,  1 , DataType.Str);
                sys_fieldvalue.V_Text = (String)Utils.GetQueryAndFormString("V_Text", this, 100,  0, DataType.Str);
                sys_fieldvalue.V_Code = (String)Utils.GetQueryAndFormString("V_Code", this, 100,  0, DataType.Str);
                sys_fieldvalue.V_ShowOrder = (Int32)Utils.GetQueryAndFormString("V_ShowOrder", this, 10,  1 , DataType.Int);
                IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
                if (Sys_FieldValueSv.Update(sys_fieldvalue) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增应用字段值
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Sys_FieldValueEntity sys_fieldvalue = new Sys_FieldValueEntity();
            sys_fieldvalue.DataTable_Action = DataTableAction.Insert;
            sys_fieldvalue.V_F_Key = (String)Utils.GetQueryAndFormString("V_F_Key", this, 50, 1, DataType.Str);
            sys_fieldvalue.V_Text = (String)Utils.GetQueryAndFormString("V_Text", this, 100, 0, DataType.Str);
            sys_fieldvalue.V_Code = (String)Utils.GetQueryAndFormString("V_Code", this, 100, 0, DataType.Str);
            sys_fieldvalue.V_ShowOrder = (Int32)Utils.GetQueryAndFormString("V_ShowOrder", this, 10, 1, DataType.Int);
            IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Concat("V_F_Key='",sys_fieldvalue.V_F_Key,"' and  V_Code='", sys_fieldvalue.V_Code, "'");
            int recordNum = 0;
            List<Sys_FieldValueEntity> sys_fieldvalueList = Sys_FieldValueSv.List(qp, out recordNum);
            if (sys_fieldvalueList.Count > 0)
            {
                return "{success:'false',message:'重复的编码'}";
            }
            if (Sys_FieldValueSv.Insert(sys_fieldvalue) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除应用字段值
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Sys_FieldValueEntity sys_fieldvalue = new Sys_FieldValueEntity();
                sys_fieldvalue.ValueID = intId;
                IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
                if (Sys_FieldValueSv.Delete(sys_fieldvalue) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除应用字段值
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Sys_FieldValueEntity> Sys_FieldValueSv = ServiceBuilder.BuildSys_FieldValueService();
                if (Sys_FieldValueSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
